//
//  LZPostWordViewController.m
//  LZHundredJie
//
//  Created by apple on 16/5/1.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

#import "LZPostWordViewController.h"
#import "LZPlaceholderTextView.h"
#import "LZPostWordToolbar.h"


@interface LZPostWordViewController () <UITextViewDelegate>

/** 文本框 */
@property (nonatomic, weak) LZPlaceholderTextView *textView;

/** 工具条 */
@property (nonatomic, weak) LZPostWordToolbar *toolbar;
@end

//  UIBarButtonItemStylePlain
@implementation LZPostWordViewController

#pragma mark - 初始化

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupTextView];
    
    [self setupToolbar];
    
}

/**
    // 重新刷新自己和子控件的所有内容(状态、尺寸)
 *  [tempView layoutIfNeeded];
    // 重新调用tempView的layoutSubviews(重新排布子控件的frame,不包括状态)
 *  [tempView setNeedsLayout];
    // 重新调用tempView的drawRect:方法(重新绘制tempView里面的内容,一般不包括子控件)
 *  [tempView setNeedsDisplay];
 */

//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    self.navigationItem.rightBarButtonItem.enabled = NO;
//}


- (void)setupToolbar{
    LZPostWordToolbar *toolbar = [LZPostWordToolbar lz_viewFromXib];
//    self.textView.inputAccessoryView = toolbar;
    toolbar.x = 0;
    toolbar.y = self.view.height - toolbar.height;
    
    // 这句代码可要可不要
    toolbar.width = self.view.width;
    [self.view addSubview:toolbar];
    self.toolbar = toolbar;
    
    // 监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

// UIKeyboardAnimationDurationUserInfoKey

// UIKeyboardFrameEndUserInfoKey

// 计算方法:工具条需要移动的距离 = 键盘最终的Y值减去屏幕的高度
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        // 工具条平移的距离 == 键盘最终的Y值 - 屏幕的高度
        CGFloat ty = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y - ScreenH;
        self.toolbar.transform = CGAffineTransformMakeTranslation(0, ty);
    }];
}

- (void)setupNav{
    self.title = @"发表文字";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStyleDone) target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:(UIBarButtonItemStyleDone) target:self action:@selector(post)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    // 强制刷新,能马上刷新现在的状态
    // 如果以后开发中，遇到设置某种状态之后应该当时就应该好使，可是不好使,
    // 可以调用layoutIfNeeded方法，来进行强制刷新，就解决了
    [self.navigationController.navigationBar layoutIfNeeded];
}

- (void)setupTextView
{
    LZPlaceholderTextView *textView = [[LZPlaceholderTextView alloc] init];
    textView.frame = self.view.bounds;
    // 不管内容有多少,竖直方向上永远可以拖拽
    textView.alwaysBounceVertical = YES;
    textView.delegate = self;
//    textView.font = [UIFont systemFontOfSize:14.0];
    textView.placeholder = @"123456789987654321123456789987654321123456789987654321123456789987654321123456789987654321123456789987654321";
    
    [self.view addSubview:textView];
    self.textView = textView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // 一进来就弹出键盘
    [self.textView becomeFirstResponder];
}

#pragma mark - 监听
- (void)post
{
    LZFuc
}

- (void)cancel {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITextViewDelegate方法
// 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)textViewDidChange:(UITextView *)textView
{
    self.navigationItem.rightBarButtonItem.enabled = textView.hasText;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
